package com.work.xxzl.module.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.work.xxzl.R;
import com.work.xxzl.utils.UIUtils;


/**
 * @author WZ 2016/12/3
 *         引导页
 */

public class GuideActivity extends AppCompatActivity {
    private ViewPager guid_viewpager;
    private int[] imags = {R.drawable.guid1, R.drawable.guid2, R.drawable.guid3};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息栏
        setContentView(R.layout.activity_guide);
        initView();
        initData();
    }


    private void initView() {
        guid_viewpager = (ViewPager) findViewById(R.id.guid_viewpager);
    }

    private void initData() {
        //这个界面加载过一次就保存标记
        guid_viewpager.setAdapter(new ViewPagerAdapter());
    }

    public class ViewPagerAdapter extends PagerAdapter {

        public ImageView imageView;

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return imags.length;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            imageView = (ImageView) View.inflate(UIUtils.getContext(),
                    R.layout.item_viewpager, null);
            imageView.setImageResource(imags[position]);
            container.addView(imageView);
            if (position + 1 == imags.length) {
                imageView.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        UIUtils.startActivity(new Intent(GuideActivity.this, MainActivity.class));
                        finish();
                    }
                });
            }

            return imageView;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        //销毁position位置的界面
        @Override
        public void destroyItem(View v, int position, Object arg2) {
            // TODO Auto-generated method stub
            ((ViewPager) v).removeView(v);
        }
    }
}
